package com.youhong.teethcare.settingpage;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.icu.util.MeasureUnit;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.youhong.cuptime.blesdk.BleService;
import com.youhong.teethcare.BaseActivity;
import com.youhong.teethcare.MainActivity;
import com.youhong.teethcare.R;
import com.youhong.teethcare.services.Common;
import com.youhong.teethcare.utils.ScreenUtils;
import com.youhong.teethcare.utils.ToastUtil;

public class SetBrushTimeActivity extends BaseActivity implements View.OnClickListener {

    ImageView iv_close;
    TextView tv_option1, tv_option2, tv_option3, tv_option4;
    Button btn_save;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_brush_time);
        getViews();
        IntentFilter filter = new IntentFilter();
        filter.addAction(BleService.ACTION_DATA_AVAILABLE);
        registerReceiver(receiver, filter);

        if (MainActivity.mDevice != null) {

            MainActivity.mDevice.getBrushingTime();
        }
        int brushTime = Common.userInfo.getBrushTime();
        if (brushTime == 90) {

            selectOption(1);
        } else if (brushTime == 120) {

            selectOption(2);
        } else if (brushTime == 150) {

            selectOption(3);

        } else if (brushTime == 180) {

            selectOption(4);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    private void getViews() {
        btn_save = findViewById(R.id.setBrushTime_btn_save);
        btn_save.setOnClickListener(this);

        iv_close = findViewById(R.id.setBrushTime_iv_close);
        iv_close.setOnClickListener(this);

        tv_option1 = findViewById(R.id.setBrushTime_tv_option1);
        tv_option1.setOnClickListener(this);


        tv_option2 = findViewById(R.id.setBrushTime_tv_option2);
        tv_option2.setOnClickListener(this);

        tv_option3 = findViewById(R.id.setBrushTime_tv_option3);
        tv_option3.setOnClickListener(this);

        tv_option4 = findViewById(R.id.setBrushTime_tv_option4);
        tv_option4.setOnClickListener(this);


    }

    private void cancelAllOption() {

        Drawable drawable1 = getResources().getDrawable(R.mipmap.icon_brushtime_unselected);
        drawable1.setBounds(0, 0, drawable1.getIntrinsicWidth(), drawable1.getIntrinsicHeight());

        tv_option1.setCompoundDrawables(drawable1, null, null, null);
        tv_option1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16.8f);
        tv_option1.setTextColor(0x5005A0FF);

        tv_option2.setCompoundDrawables(drawable1, null, null, null);
        tv_option2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16.8f);
        tv_option2.setTextColor(0x5005A0FF);

        tv_option3.setCompoundDrawables(drawable1, null, null, null);
        tv_option3.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16.8f);
        tv_option3.setTextColor(0x5005A0FF);


        tv_option4.setCompoundDrawables(drawable1, null, null, null);
        tv_option4.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16.8f);
        tv_option4.setTextColor(0x5005A0FF);

    }

    int selectedOption = 0;

    private void selectOption(int optionId) {

        cancelAllOption();
        if (optionId == 4) {

            Drawable drawable = getResources().getDrawable(R.mipmap.icon_brushtime_selected);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            tv_option4.setCompoundDrawables(drawable, null, null, null);
            tv_option4.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18.5f);
            tv_option4.setTextColor(0xFF05A0FF);
            Common.userInfo.setBrushTime(180);

        } else if (optionId == 2) {

            Drawable drawable = getResources().getDrawable(R.mipmap.icon_brushtime_selected);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            tv_option2.setCompoundDrawables(drawable, null, null, null);
            tv_option2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18.5f);
            tv_option2.setTextColor(0xFF05A0FF);
            Common.userInfo.setBrushTime(120);
        } else if (optionId == 3) {

            Drawable drawable = getResources().getDrawable(R.mipmap.icon_brushtime_selected);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            tv_option3.setCompoundDrawables(drawable, null, null, null);
            tv_option3.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18.5f);
            tv_option3.setTextColor(0xFF05A0FF);
            Common.userInfo.setBrushTime(150);
        } else {

            Drawable drawable = getResources().getDrawable(R.mipmap.icon_brushtime_selected);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            tv_option1.setCompoundDrawables(drawable, null, null, null);
            tv_option1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18.5f);
            tv_option1.setTextColor(0xFF05A0FF);
            Common.userInfo.setBrushTime(90);
        }
        Common.userInfo.SyncToLocal(getApplicationContext());
    }

    @Override
    public void onClick(View v) {

        if (iv_close == v) {

            this.finish();

        } else if (btn_save == v) {

            if (MainActivity.mDevice == null) {
                ToastUtil.showLongToast(this, R.string.strings27);
            } else {

                if (selectedOption == 4) {

                    MainActivity.mDevice.setBrushingTime(180);

                } else if (selectedOption == 3) {

                    MainActivity.mDevice.setBrushingTime(150);

                } else if (selectedOption == 2) {

                    MainActivity.mDevice.setBrushingTime(120);

                } else {

                    MainActivity.mDevice.setBrushingTime(90);
                }
                ToastUtil.showShortToast(this, R.string.strings71);
                this.finish();
            }

        } else if (tv_option1 == v) {
            selectOption(1);
            selectedOption = 1;


        } else if (tv_option2 == v) {
            selectOption(2);
            selectedOption = 2;


        } else if (tv_option3 == v) {

            selectOption(3);
            selectedOption = 3;

        } else if (tv_option4 == v) {

            selectOption(4);
            selectedOption = 4;

        }

    }

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            byte[] values = intent.getByteArrayExtra(BleService.EXTRA_DATA);
            if (values[0] == (byte) 0x07) {
                int totalSeconds = values[1] + values[2] + values[3] + values[4];

                if (totalSeconds == 90) {
                    selectedOption = 1;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            selectOption(1);
                        }
                    });
                } else if (totalSeconds == 120) {
                    selectedOption = 2;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            selectOption(2);
                        }
                    });
                } else if (totalSeconds == 150) {
                    selectedOption = 3;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            selectOption(3);
                        }
                    });
                } else {
                    selectedOption = 4;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            selectOption(4);
                        }
                    });
                }
            }
        }

    };
}